import {Injectable} from '@angular/core';
import {HttpClient, HttpEvent, HttpEventType, HttpHeaders, HttpRequest, HttpResponse} from "@angular/common/http";
import {Observable} from "rxjs";
import {map} from "rxjs/operators";

export interface Roleinfo {
  id: number;
  value: string;
}

@Injectable({
  providedIn: 'root'
})
export class RoleService {

  constructor(private httpClient: HttpClient) {
  }

  getRoleinfo(): Observable<Roleinfo[]> {
    return this.httpClient.get<Map<string, string>>('http://localhost:8080/chklogin/roleinfo').pipe(
      map(responseMap => {
        // 将Map<string, string>转换为Roleinfo[]
        return Object.entries(responseMap).map(([key, value]) => ({
          id: parseInt(key, 10), // 假设key是字符串形式的数字
          value: value
        }));
      })
    );
  }

  getLoginJWT(name: string, password: string, roleId: number): Observable<string> {

    const url = 'http://localhost:8080/login/myLogin';
    const body = JSON.stringify({
      name: name,
      password: password,
      roleId: roleId
    });
    return this.httpClient.post(url, body, {
      responseType: 'text',
      headers: {
        'Content-Type': 'application/json',
          'Accept': '*/*'
      }
    }
    );
  }
}
